package com.txsj.scan.presenter;


import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.UrlInterface;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.IAllocationCheckStatusView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AllocationCheckStatusPresenter extends BasePresenter<IAllocationCheckStatusView> {
    public AllocationCheckStatusPresenter(IAllocationCheckStatusView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    public void allocationCehckStatus(String outWarHouseId, String outWareHouseName, String billId, String billRemark, String goodsJson, String epcList) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("employee_id", "0");
        params.put("employee_name", "");
        params.put("check_status", "6");
        params.put("out_warehouse_id", outWarHouseId);
        params.put("out_warehouse_name", outWareHouseName);
        params.put("bill_id", billId);
        params.put("bill_remark", billRemark);
        params.put("goods_json", goodsJson);
        params.put("epc_list", epcList);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class)
                .allocationCheckStatus(
                        RetrofitManager.getUrl(UrlInterface.URLAllocationCheckStatus, params),
                        RetrofitManager.getHeader(),
                        RetrofitManager.getNewParams(params))
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        if (response != null) {
                            try {
                                String responBody = response.body().string();
                                JSONObject json = new JSONObject(responBody);
                                if (json.getBoolean("success")) {
                                    view.checkSuccess();
                                } else {
                                    view.checkFailed(json.getString("msg"));
                                }
                            } catch (Exception e) {

                            }

                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Trace.e("wcj请求结果:" + t.getMessage());
                    }
                });
    }


    /**
     * 获取仓库列表
     */
    public void getWarehouseHttp() {
        Map<String, String> params = RetrofitManager.getParams();
        params.put("is_show", "1");
        RetrofitManager.getDefault().create(ApiServer.class).getWareHouse(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<WarehouseBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseListResult<WarehouseBean> result) {
                        if (result.isSuccess()) {
                            view.loadWareHouseList(result.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
